package xyz.lne.researchgroupmanage.service;


import org.springframework.transaction.annotation.Transactional;
import xyz.lne.researchgroupmanage.entity.SysUser;
import com.baomidou.mybatisplus.extension.service.IService;
import xyz.lne.researchgroupmanage.service.dataTransferObject.UserDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务类
 * </p>
 *
 * @author team7
 * @since 2021-01-19
 */
// 用户服务
public interface SysUserService extends IService<SysUser> {
    //TODO:使用springsecurity来验证用户是否正确 登录及生成token等

    /**
     * list all user
     * @return
     */
    @Transactional
    List<UserDTO> listAll();


    /**
     * return the userName by userId
     * @param userId the id of the user
     * @return the userName
     */
    @Transactional
    String findNameById(Integer userId);

    /**
     * return the List of UserDTO By List of UserId
     * @param userIds
     * @return
     */
    default List<UserDTO> getUsersById(List<Integer> userIds){
        List<UserDTO> userDTOList=new ArrayList<>();
        for(Integer i:userIds){
            userDTOList.add(findUserById(i));
        }
        return userDTOList;
    }

    /**
     * return the user basic info by userId
     * @param userId the id of the user
     * @return the UserResponse entity which includes the basic info of specified user
     */
    @Transactional
    UserDTO findUserById(Integer userId);

    /**
     * add user to the database
     * @param userDTO the request which includes the basic info of the new user
     * @return the UserResponse entity which includes the basic info of the new user
     */
    @Transactional
    UserDTO addUser(UserDTO userDTO);

    /**
     * update specified user info to the database
     * @param userDTO the request which includes the info of modification
     * @return the UserResponse entity which includes the basic info of the modified user
     */
    @Transactional
    UserDTO updateUser(UserDTO userDTO);

    /**
     * find user by account
     * @param account the account of the specified user
     * @return the UserDTO entity of the specified user
     */
    @Transactional
    UserDTO findUserByAccount(String account);

    /**
     * 由于数据库中的account字段是唯一索引
     * 前端需要检查传入的account是否已经被使用
     * 当account不能使用（数据库中已有用户占用该account）返回false
     * @param account
     * @return
     */
    Boolean isAccountAvailable(String account);

    /**
     * 判断当前用户Id是否合法，合法则无动作，非法则抛出异常
     * @param userId
     */
    void isLegal(Integer userId);
}
